<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatchBrand()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  >									
									<button class="btn btn-default" >查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="dataLists">

			                      </tbody>
			                  </table>
							<div th:insert="/admin/page.html"></div>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" placeholder="规格名称" id="specName" name="specName">  </td>
		      	</tr>
			 </table>

			<!-- 规格选项 -->
			<div class="btn-group">
				<button type="button" class="btn btn-default" title="新建" onclick="make_1()" ><i class="fa fa-file-o"></i> 新增规格选项</button>
				<script type="text/javascript">
					function make_1(){
						var tab=document.getElementById("table");
						var tr=document.createElement("tr");
						tr.innerHTML='';
						tr.innerHTML +='<td><input  class="form-control" placeholder="规格选项" id="optionName" name="optionName""></td>';
						tr.innerHTML +=' <td><input  class="form-control" placeholder="排序" id="orders" name="orders"></td>';
						tr.innerHTML +='<td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
						tab.appendChild(tr);
					}

					function dalete(obj){
						var d =(obj.parentNode.parentNode);
						d.parentNode.removeChild(d);
					}
				</script>

			</div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable" id="table">
                    <thead>
                        <tr>
                          
					 
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>	
                    </thead>
                    <tbody>
                      <tr id="tr">
                      <!--新增选项-->
						<td>
							<input  class="form-control" placeholder="规格选项" id="optionName" name="optionName">
						</td>
						 <td>
							 <input  class="form-control" placeholder="排序" id="orders" name="orders"></td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
                    </tbody>
			  </table>


		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="saveSpciOption()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!--回显窗口-->
  <div class="modal fade" id="updateModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">规格编辑</h3>
			  </div>
			 <input type="hidden" class="form-control" placeholder="规格名称" id="updateId" name="id">
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px" id="table1">
					  <tr>
						  <td>规格名称</td>

						  <td><input  class="form-control" placeholder="规格名称" id="updateName" name="specName">  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="修改" onclick="make_2()" ><i class="fa fa-file-o"></i> 修改规格选项</button>
					  <script type="text/javascript">
						  function make_2(){
							  var tab=document.getElementById("updateSpecOption");
							  var tr=document.createElement("tr");
							  tr.innerHTML='';
							  tr.innerHTML +='<td><input  class="form-control" placeholder="规格选项" id="updateOptionName" name="optionName""></td>';
							  tr.innerHTML +=' <td><input  class="form-control" placeholder="排序" id="updateOrders" name="orders"></td>';
							  tr.innerHTML +='<td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
							  tab.appendChild(tr);
						  }

						  function dalete(obj){
							  var d =(obj.parentNode.parentNode);
							  d.parentNode.removeChild(d);
						  }
					  </script>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable"  id="updateSpecOption">

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpciOption()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>
<!--getSpecList// -->
	<script type="text/javascript">
	$(function () {
		getData();
	})
	function getData() {
		$.ajax({
			url: "/specification/getSpecificationList",
			type: "post",
			dataType: "json",
			async: true,
			data:{
				pageNumber:$("[name=pageNumber]").val(),
				pageSize:$("[name=pageSize]").val()
			},
			success: function (obj) {
				buildPage(obj.data)
				var result = obj.data.result;
				var datas = '';
				for (let i = 0; i < result.length; i++) {
					datas += '<tr>';
					datas += '<td><input  type="checkbox" name="box" value="'+result[i].id+'" ></td>';
					datas += '<td>'+result[i].id+'</td>';
					datas += '<td>'+result[i].specName+'</td>';
					datas += '<td class="text-center">';
					datas += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal1" onclick="toUpdateSpciOption('+result[i].id+')"  >修改</button>';
					datas += '</td>';
					datas += '</tr>';
				}
				$("#dataLists").html(datas);

			},
			error: function (datas) {
				toastr.error("品牌页面加载失败");
			}
		})
	}
</script>

<!--save-->
	<script type="text/javascript">

	function saveSpciOption() {
		var optionList = [];
		var table = $("#table")
		var trs = $(table).find("tr");
		for (let i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var option = {"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()};
			optionList.push(option);
		}
		var optionListJson = JSON.stringify(optionList);
		console.log(optionListJson);
		$.ajax({
			url: "/specification/saveSpciOption",
			type: "post",
			dataType: "json",
			async: true,
			data:{specName:$("#specName").val(),options:optionListJson},
			success: function (result) {
				if(result){
					alert("增加成功")
					window.location.reload()
				}else{
					alert("增加失败")
				}
			},
			error: function (datas) {
				alert("规格页面加载失败");
			}
		})
	}

</script>

<!--//reverse & update-->
		<script type="text/javascript">

		function toUpdateSpciOption(id) {
			$.ajax({
				url: "/specification/toUpdateSpciOption/"+id,
				type: "post",
				dataType: "json",
				async: false,
				success:function (result){
					var t = result.data.t;
					console.log(t);
					var list = result.data.list;
					$("#updateName").val(t.specName);
					$("#updateId").val(t.id);
					var content = '';
					for (var i = 0; i < list.length; i++) {
						content +='<thead>';
						content +='<tr>';
						content +='<th class="sorting">规格选项</th>';
						content +='<th class="sorting">排序</th>';
						content +='<th class="sorting">操作</th>';
						content +='</tr>';
						content +=' </thead>';
						content +=' <tbody >';
						content +='<tr>';
						content +='<td>'
						content +='<input type="hidden" value="'+list[i].id+'"  class="form-control" placeholder="规格选项">';
						content +='<input value="'+list[i].optionName+'"  class="form-control" placeholder="规格选项">';
						content +='</td>'
						content +='<td>'
						content +='<input value="'+list[i].orders+'"  class="form-control" placeholder="排序">';
						content +='</td>'
						content +='<td>'
						content +='<button type="button" class="btn btn-default" title="删除" onclick="deleteSpec('+list[i].id+')" ><i class="fa fa-trash-o"></i> 删除</button>';
						content +='</td>'
						content +='</tr>';
						content +=' </tbody >';
					}
					$("#updateSpecOption").html(content);
				},
				error:function (result){
					alert("加载失败,请联系管理员")

				},
			})
		}

//修改方法
		function updateSpciOption() {
			var options = [];
			var specName = $("#updateName").val();
			var id = $("#updateId").val();
			console.log(specName);
			var table = $("#updateSpecOption");
			var trs = $(table).find("tr");
			for (var i = 1;i < trs.length;i++){
				var tds = $(trs[i]).find("td");
				var option = {"id":$(tds[0]).find("input").val(),"optionName":$(tds[1]).find("input").val(),"orders":$(tds[1]).find("input").val()};
				options.push(option);
			}
			var OptionString = JSON.stringify(options);
			$.ajax({
				url:"/specification/updateSpciOption",
				type:"post",
				data:{specName:specName,OptionString:OptionString,id:id},
				dataType:"json",
				async:false,
				success:function (result){
					if(result){
						alert("修改成功")
						window.location.reload()
					}else{
						alert("修改失败")
					}
				},
				error:function (result){
					alert("加载失败,请联系管理员")
				},
			})
		}
	</script>

<!--delete-->
	<script type="text/javascript">
		function deleteBatchBrand() {
			var ids=[];
			$("[name=box]:checked").each(function (i,e){
				ids.push($(e).val());
			})
			alert(ids)
			$.ajax({
				url: "/specification/deleteBatchSpecOption/"+ids,
				dataType: "json",
				type: "post",
				async: true,
				success: function (result) {
					alert(result.message);
					location.reload();
				},
				error: function (result) {
					alert("系统异常,请联系管理员");
				}
			})
		}
	</script>


